package tree.tree2.symmetry.BinarySearchTree;

import tree.TreeNode;

/**
 * @ClassName SearchBST
 * @Description TODO
 * @Author lenovo
 * @Date 2023-05-05 9:56
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class SearchBST {

    /**
     * 输入：root = [4,2,7,1,3], val = 2
     *        4
     *      2   7
     *    1   3
     * 输出：[2,1,3]
     *
     * @param root
     * @param val
     * @return
     */
    public TreeNode searchBST(TreeNode root, int val) {
        if (root == null || val == root.val) return root;
        TreeNode treeNodeLeft = searchBST(root.left, val);

        if (treeNodeLeft != null) {
            return treeNodeLeft;
        }
        return searchBST(root.right, val);
    }

    public TreeNode searchBST1(TreeNode root, int val) {
        if (root == null || root.val == val) return root;

        if (root.val < val) {
            return searchBST1(root.right, val);
        } else {
            return searchBST1(root.left, val);
        }
    }

    public static void main(String[] args) {
        SearchBST searchBST = new SearchBST();
        TreeNode root = new TreeNode(1);
        root.right = new TreeNode(2);
        root.right.left = new TreeNode(3);

        System.out.println(searchBST.searchBST1(root, 2));
    }
}